package com.app.common;

import lombok.Getter;

@Getter
public class Api {
    private final Integer code;
    private final String message;
    private final Object data;

    private Api(Integer code, String message, Object data) {
        this.code = code;
        this.message = message;
        this.data = data;
    }

    public static Api create(Code code, String message, Object data) {
        return new Api(code.getCode(), message, data);
    }

    public static Api fail(Code code, String message) {
        return new Api(code.getCode(), message, null);
    }

    public static Api fail(String message) {
        return fail(Code.ERROR, message);
    }

    public static Api fail() {
        return fail("操作失败");
    }

    public static Api ok(Object data, String message) {
        return create(Code.SUCCESS, message, data);
    }

    public static Api ok(Object data) {
        return ok(data, "操作成功");
    }

    public static Api ok() {
        return ok(null);
    }

    @Getter
    public enum Code {
        SUCCESS(2000),
        AUTH(3000),
        BAD(4000),
        ERROR(5000);
        private final int code;

        Code(int code) {
            this.code = code;
        }
    }
}